#!/bin/bash

echo  ******************************create btso group and user.************************************************
group=btso
user=btso
#create group if not exists
egrep "^$group" /etc/group >& /dev/null
if [ $? -ne 0 ]
then
    groupadd $group
fi

#create user if not exists
egrep "^$user" /etc/passwd >& /dev/null
if [ $? -ne 0 ]
then
    useradd -m -g $group $user
fi 

echo  ******************************init db BTSODb for btso app start.************************************************
DB_PORT=30200
DB_IP=127.0.0.1
DB_NAME=BTSODb
DB_USER=btso
echo ================create BTSODb tablespace folder
btso_home=$btcloud_home/btso
PGSQL_HOME=$btso_home/runtime/pgsql
DB_DATA_DIR=$btso_home/pgsql
TABLESPACE_DIR=$DB_DATA_DIR/btso_tbs/

mkdir -p $TABLESPACE_DIR
cd $TABLESPACE_DIR
mkdir app_tbs ind_app_tbs
mkdir ipbook_tbs ind_ipbook_tbs
mkdir rolemgr_tbs ind_rolemgr_tbs
mkdir nwkmgt_tbs ind_nwkmgt_tbs
mkdir bsm_tbs ind_bsm_tbs
mkdir topology_tbs ind_topology_tbs
mkdir storage_tbs ind_storage_tbs
mkdir bizdata_tbs ind_bizdata_tbs
mkdir medical_tbs ind_medical_tbs
mkdir mgmgt_tbs ind_mgmgt_tbs
mkdir vm_tbs ind_vm_tbs
mkdir sysmgt_tbs ind_sysmgt_tbs
mkdir machineroom_tbs ind_machineroom_tbs
mkdir wlan_tbs ind_wlan_tbs
mkdir inspectionmgr_tbs ind_inspectionmgr_tbs
mkdir common_tbs ind_common_tbs
mkdir mma_tbs ind_mma_tbs
mkdir report_tbs ind_report_tbs
mkdir tabulardata_tbs ind_tabulardata_tbs

mkdir $DB_DATA_DIR/btso-data $DB_DATA_DIR/btso-logs
chown $DB_USER:$DB_USER -R $DB_DATA_DIR/

echo PATH================ create btso db install
su btso -c "$PGSQL_HOME/bin/pg_ctl init -D '$btso_home/pgsql/btso-data' -o '-A trust -E unicode -U root ' "

echo ================replace db conf file===
cd $btso_home/pgsql/btso-data/
echo "listen_addresses = '*'" >> postgresql.conf
echo "max_connections = 150" >> postgresql.conf
echo "port = $DB_PORT" >> postgresql.conf
echo "host all all 0.0.0.0/0 password" >> pg_hba.conf

echo ================start btso db ===
su btso -c "$PGSQL_HOME/bin/pg_ctl start -w -D '$btso_home/pgsql/btso-data/' -l '$btso_home/pgsql/btso-logs/pgsql.log' "
#su btso -c "$PGSQL_HOME/bin/pg_ctl stop -w -D '$btso_home/pgsql/btso-data/' -l '$btso_home/pgsql/btso-logs/pgsql.log' "

echo ================createuser
$PGSQL_HOME/bin/psql -d postgres -p $DB_PORT -c "CREATE USER $DB_USER with ENCRYPTED PASSWORD 'App@2020' SUPERUSER" -U root

echo ================createdb BTSODb
$PGSQL_HOME/bin/createdb -U $DB_USER -p $DB_PORT -O $DB_USER $DB_NAME
echo  ******************************init db $DB_NAME for btso app finished.*************************************************

echo  ******************************init create $DB_NAME tablespace start.*************************************************
cd $PGSQL_HOME/bin
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE app_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/app_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE ind_app_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/ind_app_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE ipbook_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/ipbook_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE ind_ipbook_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/ind_ipbook_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE rolemgr_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/rolemgr_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE ind_rolemgr_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/ind_rolemgr_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE nwkmgt_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/nwkmgt_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE ind_nwkmgt_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/ind_nwkmgt_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE bsm_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/bsm_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE ind_bsm_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/ind_bsm_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE topology_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/topology_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE ind_topology_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/ind_topology_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE storage_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/storage_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE ind_storage_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/ind_storage_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE bizdata_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/bizdata_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE ind_bizdata_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/ind_bizdata_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE medical_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/medical_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE ind_medical_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/ind_medical_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE mgmgt_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/mgmgt_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE ind_mgmgt_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/ind_mgmgt_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE vm_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/vm_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE ind_vm_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/ind_vm_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE sysmgt_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/sysmgt_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE ind_sysmgt_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/ind_sysmgt_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE machineroom_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/machineroom_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE ind_machineroom_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/ind_machineroom_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE wlan_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/wlan_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE ind_wlan_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/ind_wlan_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE inspectionmgr_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/inspectionmgr_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE ind_inspectionmgr_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/ind_inspectionmgr_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE common_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/common_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE ind_common_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/ind_common_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE mma_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/mma_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE ind_mma_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/ind_mma_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE report_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/report_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE ind_report_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/ind_report_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE tabulardata_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/tabulardata_tbs' "
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE TABLESPACE ind_tabulardata_tbs  OWNER btso LOCATION '$TABLESPACE_DIR/ind_tabulardata_tbs' "
echo  ******************************init create $DB_NAME tablespace finished.*************************************************

echo  ******************************init create $DB_NAME schema start.************************************************
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE SCHEMA app AUTHORIZATION btso"
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE SCHEMA ipbook AUTHORIZATION btso"
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE SCHEMA rolemgr AUTHORIZATION btso"
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE SCHEMA nwkmgt AUTHORIZATION btso"
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE SCHEMA bsm AUTHORIZATION btso"
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE SCHEMA rmmgt AUTHORIZATION btso"
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE SCHEMA stormgt AUTHORIZATION btso"
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE SCHEMA bizdatamgt AUTHORIZATION btso"
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE SCHEMA medicalmgt AUTHORIZATION btso"
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE SCHEMA mgmgt AUTHORIZATION btso"
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE SCHEMA vmmgt AUTHORIZATION btso"
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE SCHEMA sysmgt AUTHORIZATION btso"
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE SCHEMA mrmgt AUTHORIZATION btso"
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE SCHEMA wlanmgt AUTHORIZATION btso"
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE SCHEMA inspectionmgt AUTHORIZATION btso"
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE SCHEMA appcommon AUTHORIZATION btso"
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE SCHEMA mma AUTHORIZATION btso"
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE SCHEMA report AUTHORIZATION btso"
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -U $DB_USER  -c "CREATE SCHEMA tabulardata AUTHORIZATION btso"
echo  ******************************init create $DB_NAME schema finished.*************************************************

echo  ******************************init create $DB_NAME sql scripts start.*************************************************
btso_home=$btcloud_home/btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/app/app_qrtz2_tables.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/ipbook/app_ipbook_tables.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/ipbook/app_ipbook_init_data_linux.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/ipbook/app_ipbook_init_index.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/rolemgr/app_rolemgr_tables.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/rolemgr/app_rolemgr_cim_init_data.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/app/app_cim_tables.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/network/app_network_cim_init_data.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/network/app_network_tables.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/network/app_network_init_data_linux.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/rm/app_rm_cim_init_data.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/rm/app_rm_tables.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/rm/app_rm_init_data_linux.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/storage/app_storage_cim_init_data.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/storage/app_storage_tables.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/machineroom/app_machineroom_cim_init_data.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/machineroom/app_machineroom_tables.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/machineroom/app_machineroom_init_data_linux.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/wlan/app_wlan_cim_init_data.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/wlan/app_wlan_tables.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/wlan/app_wlan_init_data_linux.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/vm_citrix/app_vm_citrix_cim_init_data.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/vm_citrix/app_vm_citrix_tables.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/vm_citrix/app_vm_citrix_init_data_linux.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/bizdata/app_bizdata_cim_init_data.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/bizdata/app_bizdata_tables.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/inspectionmgr/app_inspectionmgr_cim_init_data.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/inspectionmgr/app_inspectionmgr_tables.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/inspectionmgr/app_inspectionmgr_init_data_linux.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/medical/app_medical_cim_init_data.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/medical/app_medical_tables.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/overhaul/app_overhaul_cim_init_data.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/overhaul/app_overhaul_tables.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/commonweb/app_commonweb_cim_init_data.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/commonweb/app_commonweb_tables.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/commonweb/app_commonweb_init_data_linux.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/mma/app_mma_cim_init_data.sql -U btso
./psql -h $DB_IP -d $DB_NAME -p $DB_PORT -f $btso_home/runtime/monitormgt_pgsql/beta/scripts/mma/app_mma_tables.sql -U btso
echo  ******************************init create $DB_NAME sql scripts finished.*************************************************

